import request from '@/utils/request'

export function login(data) {
  return request({
    url: '/api/sso/login',
    method: 'POST',
    data
  })
}

export function getInfo(token) {
  return request({
    url: '/api/sso/info',
    method: 'GET',
    params: { token }
  })
}

export function logout() {
  return request({
    url: '/api/sso/logout',
    method: 'POST'
  })
}

export function getUserList(params) {
  return request({
    url: '/api/users',
    method: 'GET',
    params
  })
}

export function getByUsername(username) {
  const params = { f_eq_username: username }
  const users = request({
    url: '/api/users',
    method: 'GET',
    params
  })

  if (users.length > 0) {
    return users[0]
  }

  return undefined
}

export function addSysUser(params) {
  return request({
    url: '/api/users',
    method: 'POST',
    data: params
  })
}

export function updateSysUser(params) {
  return request({
    url: '/api/users/' + params.id,
    method: 'PUT',
    data: params
  })
}

export function updatePassword(params) {
  return request({
    url: '/api/users/' + params.id + '/password',
    method: 'PUT',
    data: params
  })
}

export function changeSelfPassword(params) {
  return request({
    url: '/api/users/password',
    method: 'PUT',
    data: params
  })
}

export function deleteSysUser(id) {
  return request({
    url: '/api/users/' + id,
    method: 'DELETE'
  })
}

export function enableSysUser(id) {
  return request({
    url: '/api/users/' + id + '/unlock',
    method: 'POST'
  })
}

export function disableSysUser(id) {
  return request({
    url: '/api/users/' + id + '/lock',
    method: 'POST'
  })
}

export function getUserLogList(params) {
  return request({
    url: '/api/user/logs',
    method: 'GET',
    params
  })
}
